package hanxx.security.domain;

import com.alibaba.fastjson.annotation.JSONField;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * edit:韩鑫鑫
 * 部门:产品研发三部
 **/
public class SysUserLogin implements UserDetails {
    private static final long serialVersionUID = 1L;

    private SysUser sysUser;
    private Set<String> permissions;
    private Set<String> roles;

    public Set<String> getRoles() {
        return roles;
    }

    public void setRoles(Set<String> roles) {
        this.roles = roles;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    /** 通过UUID生成的用户唯一表示 */
    private String token;

    public Set<String> getPermissions() {
        return permissions;
    }


    public void setPermissions(Set<String> permissions) {
        this.permissions = permissions;
    }

    public SysUser getSysUser() {
        return sysUser;
    }

    public void setSysUser(SysUser sysUser) {
        this.sysUser = sysUser;
    }

    public SysUserLogin() {
    }

    public SysUserLogin(SysUser sysUser) {
        this.sysUser = sysUser;
    }

    public SysUserLogin(SysUser sysUser,Set<String> permissions) {
        this.sysUser = sysUser;
        this.permissions = permissions;
    }

    public SysUserLogin(SysUser sysUser, Set<String> permissions, String token) {
        this.sysUser = sysUser;
        this.permissions = permissions;
        this.token = token;
    }

    public SysUserLogin(SysUser sysUser, Set<String> permissions, Set<String> roles, String token) {
        this.sysUser = sysUser;
        this.permissions = permissions;
        this.roles = roles;
        this.token = token;
    }

    @JSONField(serialize = false)
    private List<SimpleGrantedAuthority> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        /*把permissions中String类型的权限信息封装成SimpleGrantedAuthority对象
       authorities = new ArrayList<>();
        for (String permission : permissions) {
            SimpleGrantedAuthority authority = new SimpleGrantedAuthority(permission);
            authorities.add(authority);
        }*/

        /*使用了自定义的@PreAuthorize("@ss.hasPermi('test1')")中的
        @Service("ss")
        public class PermissionService{}
        方法，便不需要向JwtAuthenticationTokenFilter类的doFilterInternal方法中写入如下的权限信息了

        UsernamePasswordAuthenticationToken authenticationToken =
                new UsernamePasswordAuthenticationToken(userLogin,null,userLogin.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);

        if(authorities!=null){
            return authorities;
        }
        authorities = permissions.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
        return authorities;*/

        return null;
    }

    @Override
    public String getPassword() {
        return sysUser.getPassword();
    }

    @Override
    public String getUsername() {
        return sysUser.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
